package com.sky.controller.user;

import com.sky.dto.AddressId;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "地址管理")
@Slf4j
@RestController
@RequestMapping("/user/addressBook")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 查询所有地址
     * @return
     */
    @ApiOperation("查询地址")
    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        log.info("查询所有地址");
        List<AddressBook> list = addressService.list();
        return Result.success(list);
    }

    /**
     * 查询默认地址
     * @return
     */
    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> one(){
        log.info("查询默认地址");
        AddressBook addressBook = addressService.one();
        return Result.success(addressBook);

    }

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @ApiOperation("新增地址")
    @PostMapping
    public Result<String> save(@RequestBody AddressBook addressBook){
        log.info("新增地址：{}",addressBook);
        addressService.insert(addressBook);
        return Result.success();
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @ApiOperation("根据id查询地址")
    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable Long id){
        log.info("根据id查询地址：{}",id);
        AddressBook addressBook = addressService.getById(id);
        return Result.success(addressBook);
    }

    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @ApiOperation("根据id删除地址")
    @DeleteMapping
    public Result<String> deleteById(@RequestParam Long id){
        log.info("根据id删除地址：{}",id);
        addressService.deleteById(id);
        return Result.success();
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @ApiOperation("根据id修改地址")
    @PutMapping
    public Result<String> update(@RequestBody AddressBook addressBook){
        log.info("根据id修改地址：{}",addressBook);
        addressService.update(addressBook);
        return Result.success();
    }

    /**
     * 根据id修改默认地址
     * @param
     * @return
     */
    @ApiOperation("根据id修改默认地址")
    @PutMapping("/default")
    public Result<String> update(@RequestBody AddressId addressId){
        log.info("根据id修改默认地址：{}",addressId.getId());
        addressService.updateById(addressId.getId());
        return Result.success();
    }



}
